package com.sgb.kjwl.view.ui.fragment.manage;

import android.graphics.Typeface;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.annotation.Nullable;
import androidx.core.util.Pair;
import androidx.databinding.DataBindingUtil;
import androidx.fragment.app.Fragment;

import com.google.android.material.tabs.TabLayout;
import com.sgb.kjwl.AppLazyFragment;
import com.sgb.kjwl.R;
import com.sgb.kjwl.callback.OnTabSelectedListener;
import com.sgb.kjwl.databinding.FragmentEnterpriseWebBinding;
import com.sgb.kjwl.di.manage.DaggerEnterpriseManageFragComponent;
import com.sgb.kjwl.di.manage.EnterpriseManageFragModule;
import com.sgb.kjwl.view.ui.adapter.home.ViewPagerAdapter;
import com.sgb.kjwl.view.ui.fragment.enterprise.NeedFragment;
import com.sgb.kjwl.viewmodel.manage.EnterpriseWebModel;
import com.swgk.core.base.di.AppComponent;

import java.util.ArrayList;
import java.util.List;

import javax.inject.Inject;

/**
 * @author 张驰
 * @date 2021/6/21 17:10
 * @Description 需求公告的企服需求板块
 * @name com.sgb.kjwl.view.ui.fragment.manage
 */
public class EnterpriseWebFragment extends AppLazyFragment {

    @Inject
    EnterpriseWebModel viewModel;
    private String websiteNo; //网站或个人No
    private int mTypeIndex = 0; // 初始化选中tab
    private FragmentEnterpriseWebBinding mBinding;
    private String websiteType; //1 企业网站 2 团队网站  3 个人网站
    private TextView selectTextView;
    public static EnterpriseWebFragment newInstance(String websiteNo, String websiteType, int position) {
        EnterpriseWebFragment fragment = new EnterpriseWebFragment();
        Bundle args = new Bundle();
        args.putString("websiteNo", websiteNo);
        args.putString("websiteType", websiteType + "");
        args.putInt("childPosition", position);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    protected void setupFragmentComponent(AppComponent appComponent) {
        super.setupFragmentComponent(appComponent);
        DaggerEnterpriseManageFragComponent.builder().appComponent(appComponent)
                .enterpriseManageFragModule(new EnterpriseManageFragModule(this))
                .build()
                .inject(this);
    }

    @Nullable
    @Override
    public View onCreateView(@NonNull LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        mBinding = DataBindingUtil.inflate(inflater, R.layout.fragment_enterprise_web, container, false);
        if (getArguments() != null) {
            websiteNo = getArguments().getString("websiteNo");
            websiteType = getArguments().getString("websiteType");
            mTypeIndex = getArguments().getInt("childPosition", 0);
        }
        return mBinding.getRoot();
    }

    @Override
    protected void initView() {
        mBinding.tab.addOnTabSelectedListener(new TabLayout.OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                if (selectTextView == null)
                    selectTextView = new TextView(getActivity());
                selectTextView.setTextSize(15);
                selectTextView.setTextColor(getResources().getColor(R.color.color_main));
                selectTextView.setText(tab.getText());
                selectTextView.setTypeface(Typeface.defaultFromStyle(Typeface.BOLD));
                tab.setCustomView(selectTextView);
            }

            @Override
            public void onTabUnselected(TabLayout.Tab tab) {
                tab.setCustomView(null);
            }

            @Override
            public void onTabReselected(TabLayout.Tab tab) {
            }
        });

        initTab(new String[]{"商标需求", "专利需求", "版权需求", "服务需求"});
    }



    private void initTab(String[] titls) {

        String categoryMark = null, categoryPatent = null, categoryCopyright = null, categoryCompany = null, categoryService = null, categoryProperty = null;
        List<Pair<String, Fragment>> mList = new ArrayList<>();
        mList.add(new Pair<>(titls[0], NeedFragment.newInstance(titls[0], categoryMark,websiteNo,websiteType)));
        mList.add(new Pair<>(titls[1], NeedFragment.newInstance(titls[1], categoryPatent,websiteNo,websiteType)));
        mList.add(new Pair<>(titls[2], NeedFragment.newInstance(titls[2], categoryCopyright,websiteNo,websiteType)));
        mList.add(new Pair<>(titls[3], NeedFragment.newInstance(titls[3], categoryService,websiteNo,websiteType)));

        mBinding.vpNeed.setAdapter(new ViewPagerAdapter(getChildFragmentManager(), mList));
        mBinding.vpNeed.setOffscreenPageLimit(titls.length);
        mBinding.tab.setupWithViewPager(mBinding.vpNeed);
        mBinding.tab.addOnTabSelectedListener(new OnTabSelectedListener() {
            @Override
            public void onTabSelected(TabLayout.Tab tab) {
                mTypeIndex = tab.getPosition();
            }
        });
        mBinding.tab.getTabAt(0).select();
        mBinding.vpNeed.setCurrentItem(mTypeIndex);
    }

    @Override
    protected void onLazyLoad() {

    }
}
